package com.alibaba.cloud.ai.example.manus.schedule.controller;

import com.alibaba.cloud.ai.example.manus.schedule.entity.ScheduleJob;
import com.alibaba.cloud.ai.example.manus.schedule.service.DynamicSchedulerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/scheduler")
public class SchedulerController {

    @Autowired
    private DynamicSchedulerService schedulerService;

    /**
     * 添加定时任务
     */
    @PostMapping("/jobs")
    public ResponseEntity<Map<String, String>> addJob(@Valid @RequestBody ScheduleJob scheduleJob) {
        try {
            schedulerService.addJob(scheduleJob);
            return ResponseEntity.ok(Map.of("message", "任务添加成功"));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Map.of("error", e.getMessage()));
        }
    }

    /**
     * 更新定时任务
     */
    @PutMapping("/jobs/{id}")
    public ResponseEntity<Map<String, String>> updateJob(@PathVariable Long id, @Valid @RequestBody ScheduleJob scheduleJob) {
        try {
            scheduleJob.setId(id);
            schedulerService.updateJob(scheduleJob);
            return ResponseEntity.ok(Map.of("message", "任务更新成功"));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Map.of("error", e.getMessage()));
        }
    }

    /**
     * 删除定时任务
     */
    @DeleteMapping("/jobs/{id}")
    public ResponseEntity<Map<String, String>> deleteJob(@PathVariable Long id) {
        try {
            schedulerService.deleteJob(id);
            return ResponseEntity.ok(Map.of("message", "任务删除成功"));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Map.of("error", e.getMessage()));
        }
    }

    /**
     * 暂停定时任务
     */
    @PostMapping("/jobs/{id}/pause")
    public ResponseEntity<Map<String, String>> pauseJob(@PathVariable Long id) {
        try {
            schedulerService.pauseJob(id);
            return ResponseEntity.ok(Map.of("message", "任务暂停成功"));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Map.of("error", e.getMessage()));
        }
    }

    /**
     * 恢复定时任务
     */
    @PostMapping("/jobs/{id}/resume")
    public ResponseEntity<Map<String, String>> resumeJob(@PathVariable Long id) {
        try {
            schedulerService.resumeJob(id);
            return ResponseEntity.ok(Map.of("message", "任务恢复成功"));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Map.of("error", e.getMessage()));
        }
    }

    /**
     * 立即执行任务
     */
    @PostMapping("/jobs/{id}/run")
    public ResponseEntity<Map<String, String>> runJobNow(@PathVariable Long id) {
        try {
            schedulerService.runJobNow(id);
            return ResponseEntity.ok(Map.of("message", "任务执行成功"));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Map.of("error", e.getMessage()));
        }
    }

    /**
     * 获取所有任务
     */
    @GetMapping("/jobs")
    public ResponseEntity<List<ScheduleJob>> getAllJobs() {
        List<ScheduleJob> jobs = schedulerService.getAllJobs();
        return ResponseEntity.ok(jobs);
    }

    /**
     * 获取任务详情
     */
    @GetMapping("/jobs/{id}")
    public ResponseEntity<ScheduleJob> getJob(@PathVariable Long id) {
        ScheduleJob job = schedulerService.getJob(id);
        if (job != null) {
            return ResponseEntity.ok(job);
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}
